package com.top.horizon.th1804app.ui.fragment;

import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.support.v7.widget.DividerItemDecoration;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.net.protocol.NetController;
import com.net.protocol.model.CommonModel;
import com.net.protocol.model.FileDetail;
import com.net.protocol.model.RecordFile;
import com.top.horizon.th1804app.R;
import com.top.horizon.th1804app.adapter.PlaylistAdapter;
import com.top.horizon.th1804app.base.BaseFragment;
import com.top.horizon.th1804app.manager.FileManage;
import com.top.horizon.th1804app.utils.LogUtil;
import com.top.horizon.th1804app.utils.ToastUitl;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.io.File;
import java.util.ArrayList;
import java.util.Collections;

public class SearchFragment extends BaseFragment implements View.OnClickListener {

    private RecyclerView rvPlaylist;
    private PlaylistAdapter playlistAdapter;
    private Button btnAllChecked;
    private ArrayList<Integer> mCheckedList = new ArrayList<>();
    private int deleteId = 0;

    private Handler mHandler = new Handler(Looper.getMainLooper()) {
        @Override
        public void handleMessage(Message msg) {

        }
    };

    @Override
    protected int getLayoutResource() {
        return R.layout.fragment_search;
    }

    @Override
    protected void initView() {

        rvPlaylist = rootView.findViewById(R.id.rv_playlist);
        rvPlaylist.setLayoutManager(new LinearLayoutManager(this.getContext()));

        btnAllChecked = rootView.findViewById(R.id.btn_all_checked);
        btnAllChecked.setOnClickListener(this);
        rootView.findViewById(R.id.btn_playback).setOnClickListener(this);
        rootView.findViewById(R.id.btn_delete).setOnClickListener(this);
    }

    @Override
    protected void initData() {
        rvPlaylist.addItemDecoration(new DividerItemDecoration(this.getContext(), DividerItemDecoration.VERTICAL));
        playlistAdapter = new PlaylistAdapter(FileManage.getInstance().getResultData(), this.getContext());
        playlistAdapter.setPlayListAdapterListener(new PlaylistAdapter.PlayListAdapterListener() {
            @Override
            public void checkedPosition(ArrayList<Integer> checkedList) {
                Collections.sort(checkedList);
                Collections.reverse(checkedList);
                mCheckedList = checkedList;
            }

            @Override
            public void onClickChildView(RecordFile file) {
                showProgressLoadingDialog();
                NetController.checkDetail(file.name);
                //test start
                mHandler.postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        FileDetail detail = new FileDetail();
                        detail.name = "aaaaaa";
                        detail.size = 10000;
                        detail.startTime = "2018-08-08 12:12:12";
                        detail.endTime = "2018-08-08 12:12:12";
                        detail.tags = new String[]{"aaaaaa", "sssssss", "adddd", "ddd"};
                        EventBus.getDefault().post(detail);
                    }
                }, 2_000);
                //test end
            }

        });
        rvPlaylist.setAdapter(playlistAdapter);
    }

    @Override
    public void onResume() {
        super.onResume();
        if (!EventBus.getDefault().isRegistered(this)) {
            EventBus.getDefault().register(this);
        }
    }

    @Override
    public void onPause() {
        super.onPause();
        if (EventBus.getDefault().isRegistered(this)) {
            EventBus.getDefault().unregister(this);
        }
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onCommonEvent(CommonModel commonAck) {
        dismissProgressLoadingDialog();
        if (commonAck.code == 0) {
            showToast("删除成功");
            FileManage.getInstance().removeSelected();
            playlistAdapter.notifyDataSetChanged();
        } else {
            showToast("操作失败");
        }

    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onDetailEvent(FileDetail fileDetail) {
        dismissProgressLoadingDialog();
        View inflate = View.inflate(getContext(), R.layout.dialog_file_details, null);
        ((TextView) (inflate.findViewById(R.id.tv_file_name))).setText(fileDetail.name);
        ((TextView) (inflate.findViewById(R.id.tv_file_size))).setText(String.valueOf(fileDetail.size));
        ((TextView) (inflate.findViewById(R.id.tv_start_time))).setText(fileDetail.startTime);
        ((TextView) (inflate.findViewById(R.id.tv_end_name))).setText(fileDetail.endTime);
        StringBuffer buffer = new StringBuffer();
        if (fileDetail.tags != null && fileDetail.tags.length != 0) {
            for (int i = 0; i < fileDetail.tags.length; i++) {
                buffer.append(fileDetail.tags[i] + " ");
            }
        }
        ((TextView) (inflate.findViewById(R.id.tv_tag_name))).setText(buffer.toString());
        showDialog(inflate);
    }

    /**
     * 对话框内的点击监听
     *
     * @param view
     * @param mLayoutId
     */
    @Override
    public void onClickChildView(View view, int mLayoutId) {
        if (view.getId() == R.id.rl_delete_root) {
            view.findViewById(R.id.btn_confirm_delete).setOnClickListener(this);
            view.findViewById(R.id.btn_cancel).setOnClickListener(this);
            return;
        }
        view.findViewById(R.id.btn_close_dialog).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismissDialog();
            }
        });
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn_all_checked:
                //全选
                FileManage.getInstance().setAllSelected();
                playlistAdapter.notifyDataSetChanged();
                break;
            case R.id.btn_playback:
                //点击了回放
                if (FileManage.getInstance().getSelectedSize() == 0) {
                    showToast("没有选中文件");
                    return;
                }
                FileManage.getInstance().assemblePlayData();
                if (mOnClickPlayBackListener != null)
                    mOnClickPlayBackListener.onClickPlayBack();
                break;
            case R.id.btn_play_list:

                break;
            case R.id.btn_delete:
                int selectedSize = FileManage.getInstance().getSelectedSize();
                if (selectedSize == 0) {
                    showToast("没有选中文件");
                    return;
                }
                showChooseDialog(R.layout.dialog_delete_file);
                break;
            case R.id.btn_confirm_delete:
                //点击了删除
                dismissDialog();
                String[] strings = new String[FileManage.getInstance().getSelectedSize()];
                FileManage.getInstance().putDeleteFile(strings);
                deleteId = NetController.deleteFile(strings);
                showProgressLoadingDialog();
                //test begin
                mHandler.postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        CommonModel common_ack = new CommonModel();
                        common_ack.code = 0;
                        EventBus.getDefault().post(common_ack);
                    }
                }, 3_000);
                //test end
                break;
            case R.id.btn_cancel:
                dismissDialog();
                break;
        }
    }

    private OnClickPlayBackListener mOnClickPlayBackListener;//2、定义接口成员变量

    //定义接口变量的set方法
    public void setOnClickPlayBackListener(OnClickPlayBackListener onClickPlayBackListener) {
        this.mOnClickPlayBackListener = onClickPlayBackListener;
    }

    //1、定义接口
    public interface OnClickPlayBackListener {
        void onClickPlayBack();
    }
}
